package com.example.rabbitmq.limit.ack;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;
import java.util.Properties;

/**
 * @author weiyx
 * @date 2021-01-16
 */
public class MyConsumer extends DefaultConsumer {

    private Channel channel;

    public MyConsumer(Channel channel) {
        super(channel);
        this.channel = channel;
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties basicProperties,  byte[] body) throws IOException {
        System.out.println("----- consumer message -----");
        System.out.println("body = " + new String(body));

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //channel.basicAck(envelope.getDeliveryTag(), false);

        if((Integer)basicProperties.getHeaders().get("num") == 3){
            //multiple 是否批量,  requeue 是否重回队列
            channel.basicNack(envelope.getDeliveryTag(), false,true);
        }else{
            //手动签收
            channel.basicAck(envelope.getDeliveryTag(), false);
        }
    }
}
